import { signInPwd, logout, getUserInfo } from "@/api/user";
import { getMenu } from "@/api/role/index";

import { getToken, setToken, removeToken } from "@/utils/auth";
import { resetRouter } from "@/router";

const getDefaultState = () => {
  return {
    token: getToken(),
    name: "",
    avatar: "",
    roles: [],
    menus: [],
    buttonList: [],
  };
};

const state = getDefaultState();

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState());
  },
  SET_TOKEN: (state, token) => {
    state.token = token;
  },
  SET_NAME: (state, name) => {
    state.name = name;
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles;
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar;
  },
  SET_MENUS: (state, menus) => {
    state.menus = menus;
  },
  SET_BUTTONLIST: (state, buttonList) => {
    state.buttonList = buttonList;
  },
};

const actions = {
  // user login
  login({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      signInPwd(userInfo)
        .then((res) => {
          if (res.code == 200) {
            let data = res.data;
            commit("SET_TOKEN", data.token);
            setToken(data.token);
            resolve(data);
            // console.log(data)
          }
        })
        .catch((error) => {
          reject(error);
          console.log(error);
        });
    });
  },

  getrole({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      getMenu()
        .then((res) => {
          if (res.code == 200) {
            const { data } = res;
            commit("SET_MENUS", data);
            resolve(data);
            // console.log(data)
          }
        })
        .catch((error) => {
          reject(error);
          console.log(error);
        });
    });
  },

  // get user info
  getInfos({ commit, state }) {
    return new Promise((resolve, reject) => {
      getUserInfo(state.token)
        .then((response) => {
          const { data } = response;
          console.log(data);
          if (!data) {
            return reject("Verification failed, please Login again.");
          }
          const { role_name, permissions } = data;
          commit("SET_NAME", role_name);
          commit("SET_BUTTONLIST", permissions);
          resolve(data);
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token)
        .then(() => {
          removeToken(); // must remove  token  first
          resetRouter();
          commit("RESET_STATE");
          commit("SET_ROLES", []);
          resolve();
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

  // remove token
  resetToken({ commit }) {
    return new Promise((resolve) => {
      removeToken(); // must remove  token  first
      commit("RESET_STATE");
      resolve();
    });
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
